package com.cheetah.start.module.monitor.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cheetah.start.common.base.query.PageQuery;
import com.cheetah.start.module.monitor.domain.SysLog;
import com.cheetah.start.module.monitor.mapper.SysLogMapper;
import com.cheetah.start.module.monitor.service.ISysLogService;
import org.springframework.stereotype.Service;

/**
 * @author xjh
 * @create 2021/7/4
 **/
@Service
public class SysLogServiceImpl extends ServiceImpl<SysLogMapper, SysLog> implements ISysLogService {

    @Override
    public Page<SysLog> pageList(PageQuery pageQuery, SysLog sysLog) {
        Page<SysLog> pageList = new Page<>(pageQuery.getPage(), pageQuery.getLimit());
        LambdaQueryWrapper<SysLog> queryWrapper = getQueryWrapper(sysLog);
        page(pageList, queryWrapper);
        return pageList;
    }

    private LambdaQueryWrapper<SysLog> getQueryWrapper(SysLog sysLog) {
        LambdaQueryWrapper<SysLog> queryWrapper = Wrappers.lambdaQuery(SysLog.class);
        queryWrapper.orderByDesc(SysLog::getCreateTime);
        if (sysLog != null) {
            String loggingType = sysLog.getLoggingType();
            queryWrapper.eq(StrUtil.isNotBlank(loggingType), SysLog::getLoggingType, loggingType);

        }
        return queryWrapper;
    }

}
